package com.example.shop.adapter;

import android.content.Context;
import android.graphics.Bitmap;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.TextView;

import com.example.shop.R;
import com.example.shop.entity.TopicActivityEntity;
import com.nostra13.universalimageloader.core.DisplayImageOptions;
import com.nostra13.universalimageloader.core.ImageLoader;
import com.nostra13.universalimageloader.core.display.RoundedBitmapDisplayer;

import java.util.List;

public class ItemLvActivityTopicAdapter extends BaseAdapter {

    private List<TopicActivityEntity.TopicEntity> mEntities;

    private Context context;
    private LayoutInflater layoutInflater;
    private DisplayImageOptions options;

    public ItemLvActivityTopicAdapter(Context context, List<TopicActivityEntity.TopicEntity> entities) {
        this.context = context;
        this.layoutInflater = LayoutInflater.from(context);
        this.mEntities = entities;
        options = new DisplayImageOptions.Builder()
                .cacheInMemory(true)
                .cacheOnDisk(true)
                .considerExifParams(true)
                .bitmapConfig(Bitmap.Config.RGB_565)
                .displayer(new RoundedBitmapDisplayer(40))
                .build();
    }

    @Override
    public int getCount() {
        return mEntities.size();
    }

    @Override
    public TopicActivityEntity.TopicEntity getItem(int position) {
        return mEntities.get(position);
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(int position, View convertView, ViewGroup parent) {
        if (convertView == null) {
            convertView = layoutInflater.inflate(R.layout.item_lv_activity_topic, parent, false);
            convertView.setTag(new ViewHolder(convertView));
        }
        initializeViews((TopicActivityEntity.TopicEntity) getItem(position), (ViewHolder) convertView.getTag());
        return convertView;
    }

    private void initializeViews(TopicActivityEntity.TopicEntity entity, ViewHolder holder) {
        //TODO implement
        holder.tvItemLvActivityTopic.setText(entity.getName());
        ImageLoader.getInstance().displayImage(entity.getPic(), holder.imgvItemLvActivityTopic,options);
    }

    protected class ViewHolder {
        private TextView tvItemLvActivityTopic;
        private ImageView imgvItemLvActivityTopic;

        public ViewHolder(View view) {
            tvItemLvActivityTopic = (TextView) view.findViewById(R.id.tv_item_lv_activity_topic);
            imgvItemLvActivityTopic = (ImageView) view.findViewById(R.id.imgv_item_lv_activity_topic);
        }
    }
}
